package Ep02_LinkedList._0160;

import Ep02_LinkedList.ListNode;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/9 9:15
 * 说明： 力扣 0160 链表相交
 * 注意：此处的相交不是指 value相等，而是指两条链表使用相同的LiseNode对象作为链表元素
 * 因此在判定交点时不应用 pointer.value，而应该使用 pointerA == pointerB
 * 此题因为需要两条链表对齐，就算是空链表二者也能满足对齐，因此不需要在程序开头对链表进行判空。
 * */

public class Mine {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB){
        ListNode curA = headA;
        ListNode curB = headB;
        int lenA = 0;
        int lenB = 0;
        while(curA!=null){
            curA = curA.next;
            lenA++;
        }
        while(curB!=null){
            curB = curB.next;
            lenB++;
        }
        int def = lenA - lenB;
        curA = headA;
        curB = headB;
        if(def > 0){
            while (def > 0){
                curA = curA.next;
                def--;
            }
        } else if (def < 0){
            while (def < 0){
                curB = curB.next;
                def++;
            }
        }

        while (curA != null){
            if (curA == curB){
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }

        return null;
    }
}
